//
//  ViewController.swift
//  单利模式
//
//  Created by 崔博 on 2017/7/24.
//  Copyright © 2017年 崔博. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        // 没有使用单利对象
        dontUseShareInstance()
    }
    // 未使用单利 需要创建大量的实例对象
    func dontUseShareInstance()  {
        
        let sever = BackupServer(name: "Server#1")
        sever.backup(ite: DateItem(type: DateItem.ItemType.Email, data: "@.qq.com"))
        sever.backup(ite: DateItem(type: DateItem.ItemType.Phone, data: "@11111111"))
        
        globalLogger.log(msg: "Backed up 2items\(sever.name)")
        let otherServer = BackupServer(name: "Server#2")
        otherServer.backup(ite: DateItem(type: DateItem.ItemType.Email, data: "@163.cim"))

        globalLogger.log(msg: "Backed up 2items\(otherServer.name)")
        
        globalLogger.prifting()
        
    }
}

class DateItem {
    enum ItemType: String {
        case Email = "Email Address"
        case Phone = "Telephone Nhmber"
        case Card = "Credit Card Number"
    }
    
    var type: ItemType
    var data : String = ""
    init(type: ItemType, data: String) {
        self.type = type
        self.data = data
    }
}
// 给服务端添加日志功能
class BackupServer {
    var name: String = ""
    private var data = [DateItem]()
    let logger = Logger()
    init(name: String) {
        self.name = name
        globalLogger.log(msg: "Creat new server\(name)")
    }
    func backup(ite: DateItem) {
        data.append(ite)
        globalLogger.log(msg: "\(name) backed up item\(ite.type.rawValue)")
    }
    func getData() -> [DateItem] {
        return data
    }
}
let globalLogger = Logger()
final class Logger {
    private var data = [String]()
    func log(msg: String) {
        data.append(msg)
    }
    
    func prifting() {
        for msg in data {
            print("log:\(msg)")
        }
    }
}
